/**
 * wmr_local_db.js
 *  class object to controll localStorage. This class contains CRUD method, and
 *  will contain server-side synchronization method near future.
 *
 *  @version:: 0.01
 *  @author:: Kensaku KOMATSU (kensaku.komatsu@gmail.com)
 *  @last_updated:: 2010/3/20 (JL006)
 */

var wmrLocalDB = function(serviceName, attrs){
	this.initialize(serviceName, attrs);
};

/**
	{
		id : {
			srv_id : integer, // サーバーと同期がとれたときに付与される
			sync_flag : boolean, // offline状態で操作された場合はfalse、サーバーと同期がとれるとtrueになる
			remove_flag : boolean, // 通常時は、false。Deleteかつ、offlineの時は、このフラグがtrueになる
			title : string,
			price : integer,
			author : string
		}, ...
	}
*/

wmrLocalDB.prototype = {
	serviceName: null,
	attrs: [],
	
	initialize: function(serviceName, attrs) {
		if(typeof(serviceName) != 'string' || !(attrs instanceof Array))
			return null; // 例外を起こすほうがベター
		
		
		this.serviceName = serviceName;
		this.attrs = attrs;
		try {
			if (!localStorage[serviceName] || !(JSON.parse(localStorage[serviceName]) instanceof Array)) 
				localStorage[serviceName] = JSON.stringify([]);
		} catch(e) {
			localStorage[serviceName] = JSON.stringify([]);
		}
	},
	say : function() {
		var mesg = this.serviceName+","+JSON.stringify(this.attrs);
		if(console.log)
			console.log(mesg);
		else
			alert(mesg);
	},
		
	create : function(obj) {
		try {
			if(typeof(obj) != 'object') {
				var mesg = 'obj ('+JSON.stringify(obj)+') is not object';
				if(console.log) console.log(mesg);
				return false;
			}
				
			var tmp = { 
				srv_id : -1,
				sync_flag : false,
				remove_flag: false,
			};

			for(var i in this.attrs) {
				key = this.attrs[i];
				tmp[key] = obj[key] || null;
			}
			if(console.log) console.log(JSON.stringify(tmp));
			
			if(console.log) console.log(JSON.stringify(tmp));
			
			
			var db = JSON.parse(localStorage[this.serviceName]);
			db.push(tmp);
			localStorage[this.serviceName] = JSON.stringify(db);
			
			return (db.length - 1);
		} catch(e) {
			if(console.log) console.log(e);
			return false;
		}
	},
	
	// if(!id) show all.
	find : function(id) {
		try {
			if (typeof(id) != 'undefined' && typeof(id) != 'number') {
				if(console.log)
					console.log("find:: mismatch type of id => " + typeof(id));
				return false;
			}
			var db = JSON.parse(localStorage[this.serviceName]);
			
			if(typeof(id) == 'undefined') {
				var ret = [];
				for(var id in db) {
					if(db[id].remove_flag == false)
						ret[id] = db[id];
				}
				return ret;
//				return db;
			} else {
				if(id in db)
					return (db[id].remove_flag ? null : db[id]);
				else {
					if(console.log)
						console.log('id: '+id+' is not exist in this table.');
					return false;
				}
			}
		} catch(e) {
			if(console.log) console.log(e);
			return false;
		}
	},

	srv_id : function(id) {
		try {
			if (typeof(id) != 'number') {
				if(console.log)
					console.log("find:: mismatch type of id => " + typeof(id));
				return false;
			}
			var db = JSON.parse(localStorage[this.serviceName]);
			
			if(id in db)
				return (db[id].remove_flag ? null : db[id].srv_id);
			else {
				if(console.log)
					console.log('id: '+id+' is not exist in this table.');
				return false;
			}
		} catch(e) {
			if(console.log) console.log(e);
			return false;
		}
	},
	
	update : function(id, obj) {
		try {
			if(typeof(id) != 'number') {
				var mesg = 'id ('+id+') is not number';
				if(console.log) console.log(mesg);
				return false;
			}
			if(typeof(obj) != 'object') {
				var mesg = 'obj ('+JSON.stringify(obj)+') is not object';
				if(console.log) console.log(mesg);
				return false;
			}
				
			var db = JSON.parse(localStorage[this.serviceName]);
			
			if(! id in db) {
				var mesg = 'id ('+id+')not found';
				if(console.log) console.log(mesg);
				return false;
			}
			
			for(var key in obj) {
				if(key in db[id]) {
					if(typeof(obj[key]) != 'object')
						db[id][key] = obj[key];
				}
			}
			if(console.log)
				console.log("update::"+id+" "+JSON.stringify(db[id]));

			localStorage[this.serviceName] = JSON.stringify(db);
			return id;
		} catch(e) {
			if(console.log) console.log("update"+e);
			return false;
		}
	},
	
	destroy : function(id) {
		try {
			if(typeof(id) != 'number') {
				var mesg = 'id ('+id+') is not number';
				if(console.log) console.log(mesg);
				return false;
			}

			var db = JSON.parse(localStorage[this.serviceName]);
			if(!db[id]) {
				var mesg = 'id ('+id+')not found';
				if(console.log) console.log(mesg);
				return false;
			}
			
			db[id].remove_flag = true;
			
			localStorage[this.serviceName] = JSON.stringify(db);
			
			return id;
		} catch(e) {
			if(console.log) console.log(e);
			return false;
		}
	},
	
	reset : function() {
		try {
			delete localStorage[this.serviceName];
			localStorage[this.serviceName] = JSON.stringify([]);
		
			return true;
		} catch(e) {
			if(console.log) console.log(e);
			return false;
		}
	}
	
}

